package com.cobla.struct.matrix;

/**
 * 矩阵
 */
public class Matrix {

    /**
     * 给定稀疏矩阵 {@param A}, 建立对应的三元组并输出
     * @param A
     */
    public static void createTrimat(float[][] A){
        if (A == null) {
            return;
        }
        if (A.length == 0) {
            return;
        }
        int m = A.length;
        int n = A[0].length;
        float[][] B = new float[m*n][3];
        int k=1;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (A[i][j] != 0) {
                    // data
                    B[k][0] = A[i][j];
                    // row number
                    B[k][1] = i;
                    // col number and k add one
                    B[k++][2] = j;
                }
            }
        }
        // the first row records total information
        B[0][0] = k - 1;
        B[0][1] = m;
        B[0][2] = n;
        printMatrix(B);
    }

    private static void printMatrix(float[][] b) {
        System.out.println("总共 "+b[0][0]+"个元素, "+b[0][1]+" 行 "+b[0][2]+" 列");
        for (int i = 1; i < b.length; i++) {
            System.out.println("row " + b[i][1] + " ,col " + b[i][2] + " ,data " + b[i][0]);
        }
    }
}
